from pms.common.views import PostFormula
from project.models import Project
from user.models import User
from project.utils.member import project_update_member
from project.utils.profile import member_view
from project.utils.permission import assert_project_permissions


class API(PostFormula):
    def handle(self, req):
        uid = req['uid']
        pid = req['pid']
        mod = req['mod']
        assert_project_permissions(pid, self.user.id, [3])  # 注意此处 uid 为请求者，并非请求体中的 uid 参数
        project, member = project_update_member(
            project=Project.objects.get(id=pid),
            user=User.objects.get(id=uid),
            view=mod
        )
        return self.success_respond(member_view(project, member))
